<?php
class App_User_Service_Role
{
	/**
	 * @var App_User_Model_Role
	 */
	protected $_modelRole;
	
	/**
	 * @return void
	 */
	public function __construct() 
	{
		$this->_modelRole = Main_Model::factory('User_Model_Role');	
	}
	
	/**
	 * @var App_User_Model_Role
	 */
	public function getModelRole()
	{
		return $this->_modelRole;
	}

	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data)
	{
		$langId = Zend_Registry::getInstance()->langId;
		$data += array('lang_id' => $langId);
		return $this->_modelRole->insertItem($data);
	}
		
	/**
	 * @param array $data
	 * @param int $roleId
	 * @return bool
	 */
	public function updateItem($data, $roleId)
	{
		$langId = Zend_Registry::getInstance()->langId;
		$data += array('lang_id' => $langId);
		return $this->_modelRole->updateItem($data, $roleId);
	}
	
	/**
	 * @param int $id
	 * @return array
	 */
	public function getItemById($id) 
	{
		$langId = Zend_Registry::getInstance()->langId;
		return $this->_modelRole->getItemById($id, $langId);
	}
	
	/**
	 * @param string $code
	 * @return bool
	 */
	public function getItemByCode($code) 
	{
		$langId = Zend_Registry::getInstance()->langId;
		return $this->_modelRole->getItemByCode($code, $langId);
	}
	
	/**
	 * @param int $id
	 * @return bool
	 */
	public function deleteItem($id) 
	{
		$item = $this->getItemById($id);
		if ( !empty($item) && ($item['code'] == 'admin') ){
			return false;
		}
		
		return $this->_modelRole->deleteItem($id);
	}
	
	/**
	 * @return array
	 */
	public function getAllIdAndName() 
	{
		$langId = Zend_Registry::getInstance()->langId;
		return $this->_modelRole->getAllIdAndName($langId);
	}
	
	/**
     * @param string $value 
     * @param null $otherCond 
     * @return bool
     */
    public function checkUnique ($value, $otherCond = null)
    {
        $list = $this->getItemByCode($value);
        return empty($list) ? true : false;
    }
}
